﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Data;
using System.Net;
using System.IO;
using System.Configuration;

/// <summary>
///BasePage 的摘要说明
/// </summary>
public class BasePage : System.Web.UI.Page
{
    protected Access access = new Access();
    protected override void OnPreInit(EventArgs e)
    {
        if (Request.Url.AbsoluteUri.IndexOf("Login") == -1)
        {
            if (CurrentUserInfo == null)
            {
                Response.Redirect(ResolveUrl("~/Login.aspx"));
            }
        }
        base.OnPreInit(e); 
    }
   
    #region 下属页面公用方法
    public string GetQeuryString(string para)
    {
        if (Request.QueryString[para] != null) return Request.QueryString[para].ToString();
        else return "";
    }
    public void AlertAndClose(string message)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert(\"" + message + "\");closeDialog();</script>");
    }
    /// <summary>
    /// 在客户端弹出对话框
    /// </summary>
    public void AlertMessage(string message)
    {
        Page.RegisterStartupScript("AlertMessage", "<script language='javascript'>alert(\"" + message.Replace("'", "′") + "\");</script>");
    }
    /// <summary>
    /// treeGrid
    /// </summary>
    public void AlertAndCloseAndReloadParend(string message,string parentControlId)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertCloseReload", "<script>alert(\"" + message + "\");closeDialog(); parent.$('#" + parentControlId + "').treegrid(\"reload\");parent.parent.$('#treeMenu').tree(\"reload\");</script>");
    }

    /// <summary>
    /// 再客户端显示一条信息，然后关闭页面
    /// </summary>
    /// <param name="message"></param>
    protected void AlertClose(string message)
    {
        Page.RegisterStartupScript("AlertClose", "<script language='javascript'>alert(\"" + message.Replace("'", "′") + "\");window.opener=null;window.close();</script>");
    }

    protected void AlertAndCloseAndReloadParendDataGrid(string message, string parentControlId)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertCloseReload", "<script>alert(\"" + message + "\");closeDialog(); parent.$('#" + parentControlId + "').datagrid(\"reload\");</script>");
    }
    #endregion

    #region 下属页面公用属性
    protected UserInfo CurrentUserInfo
    {
        get
        {
            if (Session["UserInfo"] != null)
            {
                return (UserInfo)Session["UserInfo"];
            }
            else
            {
                //Response.Redirect("Login.aspx");
                return null;
            }
        }
    }

    public string Skin
    {
        get
        {

            return CurrentUserInfo.Skin;
        }
    }
    #endregion

    /// <summary>
    /// 导出excel
    /// </summary>
    /// <param name="ds"></param>
    /// <param name="FileName"></param>
    public void CreateExcel(DataSet ds, string FileName)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (ds != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "UTF-8";
            curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(FileName)) + ".xls");

            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dgExport = new System.Web.UI.WebControls.DataGrid();
            dgExport.DataSource = ds.Tables[0].DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    }


    public string GetAppid()
    {
        //return "wx9283e34e38cd93ee";//ntt
        //return "wx8ecea9aaa3d6162b";
        string id = ConfigurationManager.AppSettings["appid"].ToString();
        return id;

    }

    public string GetSecret()
    {
        //return "4c3fd2fe83b8af3d8321c48774598ed1";
        //return "6766f98e57832e81f1dc00cbefb15221";
        string s = ConfigurationManager.AppSettings["appsecret"].ToString();
        return s;
    }

    public string Token()
    {
        string Access_token = "";
        string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + GetAppid() + "&secret=" + GetSecret();
        try
        {
            string token = PostRequest(url);
            if (token.Contains("7200"))
            {
                string[] b = token.Split('\"');
                Access_token = b[3];
            }
        }
        catch (Exception)
        {
            Access_token = "";
        }
        return Access_token;
    }
    public string GetURL()
    {
       string url= ConfigurationManager.AppSettings["caidan"].ToString();
       return url;
        //return "m.shanghaivive";
        //return "122.226.44.60";
    }

    public string PostRequest(string url)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);  //定义请求对象，并设置好请求URL地址
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();    //定义响应对象，request在调用GetResponse方法事执行请求了，而不是在HttpWebRequest.Create的时候执行。
        Stream stream = response.GetResponseStream(); //定义一个流对象，来获取响应流
        StreamReader sr = new StreamReader(stream);  //定义一个流读取对象，读取响应流
        string responseHTML = sr.ReadToEnd();
        return responseHTML;
    }
}